package com.shao.app.controller;

import com.shao.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author PaoFu
 */
@RestController
public class WalletController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户钱包余额接口
     * @param userId
     * @return
     */
    @GetMapping("/user/{userId}/wallet")
    public Map<String, Object> getWalletBalance(@PathVariable Long userId) {
        Map<String, Object> response = new HashMap<>();
        response.put("balance", userService.getWalletBalance(userId));
        return response;
    }

    /**
     * 用户消费接口
     * @param userId
     * @param request
     * @return
     */
    @PostMapping("/user/{userId}/wallet/consume")
    public Map<String, Object> consumeWallet(@PathVariable Long userId, @RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        double amount = (double) request.get("amount");
        userService.consumeWallet(userId, amount);
        response.put("status", "success");
        return response;
    }
}
